package fr.playsoft.psas;

import java.util.Timer;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class CarrouselTop extends Activity {
    
	public static final String COLOR_SUBSERVICE_CARROUSEL_BG = "#77616781";
	
	public static final String mServiceL2ArrTitle[] = {
		"Actualites",
		"Agenda",
		"Culture",
		"Emploi",
		"Families",
		"Jeunes"
	};
	
	public static final int mServiceL2ArrResNormalPush[] = {
		R.drawable.actualites_focus,
		R.drawable.agenda_focus,
		R.drawable.culture_focus,
		R.drawable.emploi_focus,
		R.drawable.familles_focus,
		R.drawable.jeunes_focus
	};
	
	public static final int mServiceL2ArrResNormalDefault[] = {
		R.drawable.actualites,
		R.drawable.agenda,
		R.drawable.culture,
		R.drawable.emploi,
		R.drawable.familles,
		R.drawable.jeunes
	};
	
	public static final int mServiceL2ArrResLargePush[] = {
		R.drawable.actualites_big,
		R.drawable.agenda_big,
		R.drawable.culture_big,
		R.drawable.emploi_big,
		R.drawable.familles_big,
		R.drawable.jeunes_big
	};
	
	public TabHost mTabService;
	public Gallery mGalleryService;
	public CarrouselAdapter mGalleryAdapter;
	public LinearLayout mMainToast;
	public View mMainToastBottomView;
	public ImageView mMainToastImg;
	public TextView mMainToastTxt;
	
	public int mFocusPosition;
	public int mLastFocusPosition;
	
	public View mLastFocusView;
	public Dialog mDialogImage;
	public Timer mTimerShowImage;
	
	private int wait = 0;
	private boolean finish = true;
	
	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		setContentView(R.layout.carrousel_top);
		
		setupGallery();
		setupTabbar();
		setupToastPopupDialog();
	}
	
	public TextView getScrollTv() {
		return (TextView) findViewById(R.id.base_service_l2_title);
	}
	public String getCurrentTabTitle() {
		return mServiceL2ArrTitle[mFocusPosition];
	}
	
	private void showToastOnItemSelected2(int position) {
		mMainToast.setVisibility(View.VISIBLE);
		mMainToastImg.setImageDrawable(null);
		mMainToastImg.setImageResource(mServiceL2ArrResLargePush[position]);
		mMainToastTxt.setText(getCurrentTabTitle());
		
	}
	
	private void setupToastPopupDialog() {
		mMainToast = (LinearLayout) findViewById(R.id.main_toast);
		mMainToastBottomView = findViewById(R.id.mtoast_unused_space2);
		mMainToastImg = (ImageView) findViewById(R.id.mtoast_layout_image);
		mMainToastTxt = (TextView) findViewById(R.id.mtoast_layout_text);
		mMainToast.setVisibility(View.INVISIBLE);
		
		/*int height = getWindowManager().getDefaultDisplay().getHeight();
		if(height > 810) {
			//Motorola Milestone
			mMainToastBottomView.getLayoutParams().height = Constants.MTOAST_UNUSED_SPACE_HEIGHT2_800;
		}
		else if(height < 810 && height > 700) {
			//WVGA 480x800
			mMainToastBottomView.getLayoutParams().height = Constants.MTOAST_UNUSED_SPACE_HEIGHT2_800;
		}
		else if(height > 400 && height < 500) {
			//HVGA 320x480
			mMainToastBottomView.getLayoutParams().height = Constants.MTOAST_UNUSED_SPACE_HEIGHT2_480;
		}
		else if(height > 300 && height < 350) {
			//HVGA 320x480
			mMainToastBottomView.getLayoutParams().height = Constants.MTOAST_UNUSED_SPACE_HEIGHT2_320;
		}*/
	}
	
	private void setupTabbar() {
		mTabService = (TabHost) findViewById(R.id.tabhostService);
		mTabService.setup();
		
		addTabContent();
		
		//now, hide this tab because we don't want to show it
		mTabService.getTabWidget().setVisibility(View.GONE);
		mTabService.setCurrentTab(mFocusPosition);
		
	}
	
	public void addTabContent() {
		mTabService.addTab(createTabSpecNewsList(mServiceL2ArrTitle[0], R.id.tab0));
		mTabService.addTab(createTabSpecNewsList(mServiceL2ArrTitle[1], R.id.tab1));
		mTabService.addTab(createTabSpecNewsList(mServiceL2ArrTitle[2], R.id.tab2));
		mTabService.addTab(createTabSpecNewsList(mServiceL2ArrTitle[3], R.id.tab3));
		mTabService.addTab(createTabSpecNewsList(mServiceL2ArrTitle[4], R.id.tab4));
		mTabService.addTab(createTabSpecNewsList(mServiceL2ArrTitle[5], R.id.tab5));

	}
	
	private TabHost.TabSpec createTabSpecNewsList(String strTabTitle, int resContentId) {
		TabHost.TabSpec spec; // Reusable TabSpec for each tab
		TextView txtTabInfo; // Reusable tab indicator
		
		txtTabInfo = new TextView(this);
		txtTabInfo.setText(strTabTitle);
		txtTabInfo.setSingleLine(true);
		txtTabInfo.setTag(strTabTitle);
		txtTabInfo.setTextSize(16);
		txtTabInfo.setTypeface(Typeface.DEFAULT_BOLD);
		txtTabInfo.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

		spec = mTabService.newTabSpec(strTabTitle);
		
		spec.setContent(resContentId);

		spec.setIndicator(txtTabInfo);
		
		return spec;
	}
	
	private void setupGallery() {
		mGalleryService = (Gallery) findViewById(R.id.gallery_detail_service);
		mGalleryAdapter = new CarrouselAdapter(this, mServiceL2ArrResNormalDefault );
		
		mGalleryService.setAdapter(mGalleryAdapter);
		mGalleryService.setSelection(mFocusPosition);
		mGalleryService.setSpacing(0);
		getScrollTv().setText(getCurrentTabTitle());
		
		mGalleryService.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				
				mFocusPosition = position;
			} 
		});
		
		mGalleryService.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View v, int position, long arg3) {
				
				if(v == null) return;
				
				mFocusPosition = position;
				boolean bFirstTimeEnter = false;
				
				if(mLastFocusView!=null)
        		{	
        			ImageView img= (ImageView) mLastFocusView.findViewById(R.id.baseservice_img_gallery_item);
        			if(img != null) {
        				img.setBackgroundColor(Color.TRANSPARENT);
        				img.setImageResource(mServiceL2ArrResNormalDefault[mLastFocusPosition]);
        			}
        		}
				else {
					bFirstTimeEnter = true;
				}
		    	ImageView img1 = (ImageView) v.findViewById(R.id.baseservice_img_gallery_item);
		    	if(img1!= null) {
		    		img1.setBackgroundColor(Color.parseColor(COLOR_SUBSERVICE_CARROUSEL_BG));
		    		img1.setImageResource(mServiceL2ArrResNormalPush[position]);
		    	}
		    	
		    	getScrollTv().setText(getCurrentTabTitle());
		    	
		    	mLastFocusView = v;  
		    	mLastFocusPosition = position;
		    	
//		    	if(mTabService!= null)
//		    		mTabService.setCurrentTab(mFocusPosition);
		    	
		    	if(bFirstTimeEnter == true) {
		    		bFirstTimeEnter = false;
		    	} else {
		    		showToastOnItemSelected2(position);
		    	}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				//nothing to do here
			}
			
		});
		
		mGalleryService.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if (arg1.getAction() == MotionEvent.ACTION_UP) {		            
					
					if(finish){
						Thread thread = new Thread()
						{
						    @Override
						    public void run() {
						    	try {
				                    if(finish){
				                    	finish = !finish;
					                    wait = 0;
					                    while(wait < 1100) {
					                        Thread.sleep(100);
					                        wait += 100;
					                    }
				                    }
				                } catch(InterruptedException e) {
				                    // do nothing
				                } finally {
				                	handler.sendEmptyMessage(0);
				                }
						    }
						};
	
						thread.start();
			                    
					}      
					
					
		        }else if(arg1.getAction() == MotionEvent.ACTION_MOVE){		        	
		        	wait = 0;
		        }else if(arg1.getAction() == MotionEvent.ACTION_DOWN){		        
		        }
				return false;
			}
        });
		
	}
	
	private Handler handler = new Handler() {
        @Override
    	public void handleMessage(Message msg) {
        	mMainToast.setVisibility(View.INVISIBLE);
        	mTabService.setCurrentTab(mFocusPosition);
        	updateCurrentTabContent();
       	 	finish = true;
       	 
        }
	 };
	 
	 public void updateCurrentTabContent() {
		 //do something here
	 }
	
}